package com.wonders.hisdoctor.station.project.vaccine.service.impl;

import com.alibaba.fastjson.JSON;
import com.wonders.his.common.exception.CustomException;
import com.wonders.his.common.utils.IpUtils;
import com.wonders.his.framework.dto.LoginUserDTO;
import com.wonders.his.framework.service.TokenService;
import com.wonders.his.framework.utils.oracle.OracleDateUtils;
import com.wonders.hisdoctor.station.constant.Constants;
import com.wonders.hisdoctor.station.project.vaccine.domain.VaccineInfoDict;
import com.wonders.hisdoctor.station.project.vaccine.domain.VaccineLog;
import com.wonders.hisdoctor.station.project.vaccine.mapper.VaccineLogMapper;
import com.wonders.hisdoctor.station.project.vaccine.service.IVaccineInfoDictService;
import com.wonders.hisdoctor.station.project.vaccine.mapper.VaccineInfoDictMapper;
import com.wonders.hisdoctor.station.project.vaccine.service.IVaccineLogService;
import com.wonders.hisdoctor.station.project.vaccine.vo.VaccineInfoDictVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 疫苗基本信息表 服务类
 * </p>
 *
 * @author zhanghaonan
 * @since 2021/06/25
 */
@Service
public class VaccineInfoDictServiceImpl implements IVaccineInfoDictService {

    @Autowired
    private VaccineInfoDictMapper vaccineInfoDictMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IVaccineLogService vaccineLogService;

    @Autowired
    private OracleDateUtils oracleDateUtils;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(VaccineInfoDict vo, HttpServletRequest request) {
        //校验主键唯一
        VaccineInfoDict result = vaccineInfoDictMapper.selectById(vo);
        if (result != null) {
            throw new CustomException("疫苗代码已存在，请重新输入！");
        }
        //校验疫苗通用名称、商品名称、规格、产地联合唯一
        VaccineInfoDict search = new VaccineInfoDict();
        search.setVaccineName(vo.getVaccineName());
        search.setVaccineTradeName(vo.getVaccineTradeName());
        search.setVaccineSpec(vo.getVaccineSpec());
        search.setFirmId(vo.getFirmId());
        List<VaccineInfoDict> query = vaccineInfoDictMapper.checkQuery(vo);
        if (!CollectionUtils.isEmpty(query)) {
            throw new CustomException("疫苗通用名称、商品名称、规格、产地必须联合唯一，请重新输入！");
        }
        //获取当前用户信息
        LoginUserDTO loginUserDTO = tokenService.getLoginUserDTO(tokenService.getToken(request));
        if (loginUserDTO != null && loginUserDTO.getSysSysuserVo() != null) {
            vo.setCreateId(loginUserDTO.getSysSysuserVo().getEmpNo());
            vo.setCreateName(loginUserDTO.getSysSysuserVo().getName());
        }
        vo.setCreateTime(oracleDateUtils.getOracleSysDate());
        int count = vaccineInfoDictMapper.insert(vo);
        if (count > 0) {
            //记录日志
            vaccineLogService.saveLog(Constants.ADD, JSON.toJSONString(vo), request);
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(VaccineInfoDict vo, HttpServletRequest request) {
        //校验疫苗通用名称、商品名称、规格、产地联合唯一
        VaccineInfoDict search = new VaccineInfoDict();
        search.setVaccineName(vo.getVaccineName());
        search.setVaccineTradeName(vo.getVaccineTradeName());
        search.setVaccineSpec(vo.getVaccineSpec());
        search.setFirmId(vo.getFirmId());
        search.setVaccineCode(vo.getVaccineCode());
        List<VaccineInfoDict> query = vaccineInfoDictMapper.checkQuery(vo);
        if (!CollectionUtils.isEmpty(query)) {
            throw new CustomException("疫苗通用名称、商品名称、规格、产地必须联合唯一，请重新输入！");
        }
        //获取当前用户信息
        LoginUserDTO loginUserDTO = tokenService.getLoginUserDTO(tokenService.getToken(request));
        if (loginUserDTO != null && loginUserDTO.getSysSysuserVo() != null) {
            vo.setUpdateId(loginUserDTO.getSysSysuserVo().getEmpNo());
        }
        vo.setUpdateDate(oracleDateUtils.getOracleSysDate());
        int count = vaccineInfoDictMapper.updateById(vo);
        if (count > 0) {
            //记录日志
            vaccineLogService.saveLog(Constants.UPDATE, JSON.toJSONString(vo), request);
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(VaccineInfoDict vaccineInfoDict, HttpServletRequest request) {
        VaccineInfoDict logObj = vaccineInfoDictMapper.selectById(vaccineInfoDict);
        int count = vaccineInfoDictMapper.deleteById(vaccineInfoDict);
        if (count > 0) {
            //记录日志
            vaccineLogService.saveLog(Constants.DELETE, JSON.toJSONString(logObj), request);
        }
        return count;
    }

    @Override
    public VaccineInfoDict getById(VaccineInfoDict vaccineInfoDict) {
        return vaccineInfoDictMapper.selectById(vaccineInfoDict);
    }

    @Override
    public List<VaccineInfoDictVo> search(VaccineInfoDict vaccineInfoDict) {
        return vaccineInfoDictMapper.search(vaccineInfoDict);
    }

}
